package com.zkingsoft.actions.admin;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.constance.Dictionary;
import com.zkingsoft.model.Bug;
import com.zkingsoft.model.CodeReview;
import com.zkingsoft.model.Company;
import com.zkingsoft.model.DataDictionary;
import com.zkingsoft.model.ProjPost;
import com.zkingsoft.model.ProjUser;
import com.zkingsoft.model.Project;
import com.zkingsoft.model.Task;
import com.zkingsoft.plugin.util.MoneyUtil;
import com.zkingsoft.plugin.util.WebUtil;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.BugService;
import com.zkingsoft.services.CodeReviewService;
import com.zkingsoft.services.CompanyService;
import com.zkingsoft.services.DataDictionaryService;
import com.zkingsoft.services.ProjPostService;
import com.zkingsoft.services.ProjUserService;
import com.zkingsoft.services.ProjectService;
import com.zkingsoft.services.TaskService;

/**
 * @date 2016-11-09 09:41
 */
@Controller
@RequestMapping(value = "/admin/projectInfo")
public class ProjectInfoController  extends BaseController{
	@Resource
	private ProjectService projectService;
	@Resource
	private ProjUserService projUserService;
	@Resource
	private TaskService taskService;
	@Resource
	private BugService bugService;
	@Resource
	private CodeReviewService  codeReviewService;
	@Resource
	private ProjPostService projPostService;
	@Resource
	private CompanyService companyService;
	@Resource
	private DataDictionaryService dataDictionaryService;
	
	/**
	 *跳转到项目管理子页面
	 */
	@RequestMapping(value = "/sonManager")
	public String sonManager(Long id) {
		Project project = projectService.findById(id);
		//项目详情
		WebUtil.getRequest().setAttribute("obj",project);		
		return "admin/proj-manager-son";
	}
	
	
	/**
	 *查询项目详细信息 
	 */
	@RequestMapping(value = "/all")
	public String all(Long id) {
		Project project = projectService.findById(id);
		//项目详情
		WebUtil.getRequest().setAttribute("obj",project);
		long totalTime=project.getEndTime().getTime()-project.getCreateTime().getTime();
		long remainingTime=new Date().getTime()- project.getCreateTime().getTime();
		double proportion =MoneyUtil.mul(MoneyUtil.div(remainingTime,totalTime, 2), 100);
		WebUtil.getRequest().setAttribute("proportion", proportion);
		
		//开发人员
		ProjUser projectUser=new ProjUser();
		projectUser.setProjId(id);
		WebUtil.getRequest().setAttribute("projectUserList",  projUserService.findByModel(projectUser));
		return "admin/projectInfo-list";
	}
	//任务查询
	@RequestMapping(value = "/qTask")
	public @ResponseBody AjaxResult qTask(Task task){
		List<Task> taskList = taskService.findNoStartOrStart();//状态为未开始和已开始的任务
		if(taskList!=null && taskList.size() != 0){
			Date date = new Date();
			for(Task taskCheckStatus : taskList){				
				Date endTime = taskCheckStatus.getEndTime()==null?new Date():taskCheckStatus.getEndTime();				
				if(date.getTime()>
				endTime.getTime()){
					//当前时间大于预计结束时间 则视为超时，则计算两者相差天天数
					double overTime = (date.getTime() - endTime.getTime())/(double)(1000 * 60 * 60 * 24);//想差多少天
					DecimalFormat df = new DecimalFormat("#");//不保留小数
					df.setRoundingMode(RoundingMode.FLOOR);//不四舍五入
					taskCheckStatus.setOvertime(Long.parseLong(df.format(overTime)));
					taskService.modify(taskCheckStatus);
				}	
			}
		}	
		return new AjaxResult(AjaxResult.STATUS_OK, null,null);
	}
	//任务查询
	@RequestMapping(value = "/queryTask")
	public @ResponseBody AjaxResult projStart(Task task){
		return new AjaxResult(AjaxResult.STATUS_OK, null, null,taskService.findByModel(task),0);
	}
	//bug查询
	@RequestMapping(value = "/queryBug")
	public @ResponseBody AjaxResult queryBug(Bug bug){
		return new AjaxResult(AjaxResult.STATUS_OK, null, null,bugService.findByModel(bug),0);
	}

	//加载所有的开发人员
	@RequestMapping(value = "/queryPostName")
	public @ResponseBody AjaxResult queryPostName(long projId){
		/*ProjPost projPost=new ProjPost();
		projPost.setPostName(Dictionary.POST_NAME_KFGCS);
		List<ProjPost> projPostList = projPostService.findByModel(projPost);
		if(projPostList.size()>0){*/
			ProjUser projUser=new ProjUser();
			projUser.setProjId(projId);
			return new AjaxResult(AjaxResult.STATUS_OK, null,null,projUserService.findByModel(projUser),0);
	/*	}else{
			return new AjaxResult(AjaxResult.STATUS_ERR, null,"未查询到开发人员字段");
		}*/
		
	}
	
	/*//评审得分查询
	@RequestMapping(value = "/queryProjUser")
	public @ResponseBody AjaxResult queryProjUser(ProjUser projUser){
		return new AjaxResult(AjaxResult.STATUS_OK, null, null,projUserService.findByModel(projUser),0);
	}
*/	
	
	/**
	 *修改项目状态
	 */
	@RequestMapping(value = "/updateStatus")
	public @ResponseBody AjaxResult updateStatus(Project project){
			int i = projectService.modify1(project);
			if(i>0){
				return new AjaxResult(AjaxResult.STATUS_OK, null, null);
			}else{
				return new AjaxResult(AjaxResult.STATUS_ERR, null, "更新失败");
			}
	}
	
	
	/**
	 *编辑项目信息
	 */
	@RequestMapping(value = "/updateProj")
	public @ResponseBody AjaxResult updateProj(Project project){
		int i = projectService.modify1(project);
		if(i>0){
			return new AjaxResult(AjaxResult.STATUS_OK, null, "修改成功");
		}else{
			return new AjaxResult(AjaxResult.STATUS_ERR, null, "修改失败");
		}
	}
	/**
	 *新增项目信息
	 */
	@RequestMapping(value = "/addProj")
	public @ResponseBody AjaxResult addProj(Project project){
		int i = projectService.add(project);
		if(i>0){
			return new AjaxResult(AjaxResult.STATUS_OK, null, "添加成功");
		}else{
			return new AjaxResult(AjaxResult.STATUS_ERR, null, "添加失败");
		}
	}
	/**
	 *打开编辑页面
	 */
	@RequestMapping(value = "/openEditor")
	public String openEditor(Long id){
		Project project = projectService.findById(id);
		WebUtil.getRequest().setAttribute("project", project);
		return "admin/projectInfo-form";
	}
	
	/**
	 *加载客户信息
	 */
	@RequestMapping(value = "/queryCompany")
	public @ResponseBody AjaxResult queryCompany(){
		Company company=new Company();
		return new AjaxResult(AjaxResult.STATUS_OK, null,null,companyService.findByModel(company),0);
	}
	

}
